package com.ibeeking.found.open.b.rest.controller.v1;


import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxOpenAppTypeEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.open.b.rest.service.*;
import com.ibeeking.found.open.service.common.param.QueryOpenAuthInfoParam;
import com.ibeeking.found.open.service.common.param.WxUserInfoEncryptedParam;
import com.ibeeking.found.open.service.common.vo.OpenAuthVO;
import com.ibeeking.found.open.service.common.vo.OpenAuthViewVO;
import com.ibeeking.found.open.service.common.vo.WxOpenAuthFuncVO;
import com.ibeeking.found.open.service.common.vo.WxUserInfoDecryptedVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1/open/api/auth")
public class WxAuthController {

    @Resource
    private IWxOpenEventService wxOpenEventServiceImpl;

    @Resource
    private IWxCpTpEventService wxCpTpEventServiceImpl;

    @Resource
    private IWxCpEventService wxCpEventServiceImpl;

    @Resource
    private IWxCpDkEventService wxCpDkEventServiceImpl;

    @Resource
    private IWxCpContactEventService wxCpContactEventServiceImpl;


    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询或更新第三方授权")
    @PostMapping(value = "/queryOrUpdateOpenAuthInfo")
    public OpenAuthVO queryOrUpdateOpenAuthInfo(@RequestBody @Validated QueryOpenAuthInfoParam param) throws Exception {
        return wxOpenEventServiceImpl.queryOrUpdateOpenAuthInfo(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询或更新企微第三方应用授权")
    @PostMapping(value = "/queryWxCpTpOpenAuthInfo")
    public OpenAuthVO queryWxCpTpOpenAuthInfo()  {
        return wxCpTpEventServiceImpl.queryWxCpTpOpenAuthInfo();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询或更新企微代开模板应用授权")
    @PostMapping(value = "/queryWxCpDkOpenAuthInfo")
    public OpenAuthVO queryWxCpDkOpenAuthInfo()  {
        return wxCpDkEventServiceImpl.queryWxCpDkOpenAuthInfo();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询或更新企微通讯录授权")
    @PostMapping(value = "/queryWxCpContactOpenAuthInfo")
    public OpenAuthVO queryWxCpContactOpenAuthInfo()  {
        return wxCpContactEventServiceImpl.queryWxCpContactOpenAuthInfo();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询或更新第三方授权")
    @PostMapping(value = "/queryWxOpenAuthByType/{configType}/{configSubType}/{appType}")
    public OpenAuthViewVO queryOpenAuthByType(@PathVariable("configType") Integer configType, @PathVariable("configSubType") Integer configSubType, @PathVariable("appType") Integer appType){
        String tenantId = UserUtils.getTenantId();
        if(StringUtils.isBlank(tenantId)){
            throw new BusinessException("无法查询到租户信息");
        }
        if(configType== OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_OPEN.getCode()){
            if(configSubType== WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_MP.getCode()){
                return wxOpenEventServiceImpl.queryOpenAuthViewByAllType(Long.valueOf(tenantId), configType, configSubType, appType);
            }
            if(configSubType== WxOpenAppTypeEnum.WECHAT_OPEN_APP_TYPE_MA.getCode()){
                return wxOpenEventServiceImpl.queryOpenAuthViewByAllType(Long.valueOf(tenantId), configType, configSubType, appType);
            }
        }
        if(configType== OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode()) {
            return wxCpEventServiceImpl.queryOpenAuthViewByAllType(Long.valueOf(tenantId), configType, configSubType, appType);
        }
        return new OpenAuthViewVO();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询授权列表信息")
    @PostMapping(value = "/queryWxOpenAuthFuncList/{appId}")
    public List<WxOpenAuthFuncVO> queryWxOpenAuthFuncList(@PathVariable("appId") String appId) {
        return wxOpenEventServiceImpl.queryWxOpenAuthFuncList(appId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "解密微信登录用户加密信息")
    @PostMapping(value = "/decryptWxUserInfo")
    public WxUserInfoDecryptedVO decryptWxUserInfo(@RequestBody @Validated WxUserInfoEncryptedParam param) throws Exception {
        return wxOpenEventServiceImpl.decryptWxUserInfo(param);
    }

}
